package com.huilan.util;

import com.huilan.model.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author ghy
 * @date 2019-04-23 23:38
 **/
public class ConstantUtil {

    /**
     * 登录用户
     */
    public static User LOGIN_USER =  null;

    /**
     * 获取当前登录用户ID
     * @return
     */
    public static User getCurrentUser(){
        Authentication authentication= SecurityContextHolder.getContext().getAuthentication();
        if(authentication!=null){
            return ((User)authentication.getPrincipal());
        }else{
            return null;
        }
    }


    /**
     * 获取当前登录用户ID
     * @return
     */
    public static String getCurrentUserId(){
        Authentication authentication= SecurityContextHolder.getContext().getAuthentication();
        if(authentication!=null){
            return ((User)authentication.getPrincipal()).getUserId().toString();
        }else{
            return null;
        }
    }

    /**
     * 获取当前登录用户名称
     * @return
     */
    public static String getCurrentUserName(){
        Authentication authentication= SecurityContextHolder.getContext().getAuthentication();
        if(authentication!=null){
            return ((User)authentication.getPrincipal()).getUsername();
        }else{
            return null;
        }
    }
}
